#include<iostream>
#include<fstream>
#include <cstdlib>
#include <ctime>
#include "Deck.h"
#include <iomanip>
#include "Outcome.h"
#include <string>
#include "Bhand.h"

using namespace std;



int main (){
    ifstream inFile("data.txt");
    ofstream outFile("data.txt");
    cout<<fixed<<showpoint<<setprecision (2);
    outFile<<fixed<<showpoint<<setprecision (2);
	srand(time(NULL));//this is for the rand function

	float amountBet= 0.0;
	int numWins = 0;//this is to keep up with the number of wins
	int numLose = 0;//this is to keep up with the number of loses
	int tie = 0;//this is for the number of ties.
	float moneyWon = 0.0;//this is to keep up with how much money is won.
	float moneyLost= 0.0;//this is to keep up with how much money is lost.
	string Hresp = " ";//this is for the player hit or stand reposnse.
	bool WorL = false;//this is to tell if the game has been won or lost yet.
	Outcome results;
	int count = 0;//keeps up with how many times game has been played.
	bool Nwin = false;//this is to make sure if they have a natural win that the game doesn't ask to hit or stand.
	float money = 1000.00;
	
    
    
    
    cout<<"Welcome to Blackjack!"<<endl;
    cout<<"The rulse are simple."<<endl;
    cout<<"Get closer to 21 than the house without going over."<<endl;
    cout<<"Good Luck!!"<<endl<<endl;
    
    
    
    do
    {
           Deck deckOcards;//makes a new shuffled deck of cards.
           Hand house;
           Hand player;
           WorL = false;
           
           if(count < 1){
                    
               cout<<"Would you like to load your game from previous data?";
               cin>>Hresp;
               cout<<endl;
               
               if(toupper(Hresp[0]) == 'Y'){
                     inFile>>money;
                     inFile>>numWins;
                     inFile>>numLose;
                     inFile>>tie;
                     inFile>>moneyWon;
                     inFile>>moneyLost;
                     inFile>>count;//this is for how many games have been played.
                     cout<<"Your previous game has now been loaded."<<endl<<endl;
               }
           }
           
		   results.setAmount(money);  
		   if(results.findMoney() > 0){
			   cout<<"You currently have $"<<results.findMoney()<<endl;//this is to find the amount of money.
			   cout<<"Please place your bet between $10 and $500."<<endl;
			   cin>>amountBet;
			 //this is to make sure the bet is between 10 and 500 dollars.//////////////////////
			   while(amountBet > 500 || amountBet < 10){
							cout<<"Invalid bet.  Please place a bet between $10 and $500."<<endl;
							cin>>amountBet;
			   }
			}
			else{
				cout<<"You have no money left! Come back when you get some more."<<endl;
				break;
			}

           
           cout<<"The dealer now deals you two cards and himself two cards."<<endl;
           
           player.addCard(deckOcards.draw());
           player.addCard(deckOcards.draw());
           house.addCard(deckOcards.draw());
           house.addCard(deckOcards.draw());
           
     
           
           
           
          
               cout<<"Your Hand"<<endl<<player<<"  ";
               cout<<"House's Hand"<<endl;
               house.displayH_Hand();
               cout<<endl;
          
          
          
         
         //this is to check to see if the player has a natural win./////////////////////////////
           if(player.getValue() == 22 && house.getValue() != 22)
           {
                        cout<<"You have a natural 21"<<endl<<endl;
                        results.Nwin(amountBet);
                        WorL = true;
                        moneyWon = (amountBet + (amountBet * 1.5));
                        numWins++;
                        Nwin= true;
                        cout<<"You won $"<<(amountBet + (amountBet * 1.5))<<endl;
                        
           }
           else{
                   
               while(toupper(Hresp[0]) != 'S'){
                    cout<<"Would you like to hit(draw a card) or stand(stay with what you have)."<<endl;    
                    cin>>Hresp;
                    
                    if(toupper(Hresp[0]) == 'H'){
                       player.addCard(deckOcards.draw());
                       system("cls");
                        
                        cout<<"Your new hand is"<<endl;
                        player.displayHand();
                        cout<<"The House's Hand is"<<endl;
                        house.displayH_Hand();
                        
               //if players hand is ever over 21 call lose function from Outcome class.//////////////
               
                        if(player.getValue() > 21){
                             cout<<"You have gone over 21!"<<endl<<endl;
                             results.lose(amountBet);
                            WorL = true;
                             numLose++;
                             moneyLost = amountBet;
                             cout<<"You lost $"<<amountBet<<endl;
                             system("pause");
                             break;
                        }
                        if(player.getIndex() == 7 && player.getValue() < 21){
                               cout<<"You have dranw 5 cards without going over this is an automatic win!!"<<endl;
                                results.win(amountBet);
                                WorL = true;
                                numWins++;
                                moneyWon = amountBet * 2;
                                cout<<"You won $"<<amountBet * 2<<endl;
                                system("pause");
                                break;
                        }
                    }
                    
               }
               
               
               while(house.getValue() < 17 && player.getValue() < 21){
                           house.addCard(deckOcards.draw());
                           cout<<"The dealer draws a card."<<endl;
                           cout<<"Your hand is"<<endl;
                           player.displayHand() ;                           
                           cout<<"The house's new hand is"<<endl;
                           house.displayH_Hand();
                           system("pause");
                           system("cls");
                           
                           
             /////if houes goes over 21 display full hand and call win from Outcome class.///////////
                        
                           if(house.getValue() > 21){
                                cout<<"The dealer has gone over 21!"<<endl<<endl;
                                cout<<house<<endl;
                                results.win(amountBet);
                                WorL = true;
                                numWins++;
                                moneyWon = amountBet * 2;
                                cout<<"You won $"<<amountBet * 2<<endl;
                                system("pause");
                                
                           }
                           if(house.getIndex() == 7 && house.getValue() < 21){
                               cout<<"The house has drawn 5 cards without going over this is an automatic win!!"<<endl;
                               cout<<house<<endl;
                                results.lose(amountBet);
                                WorL = true;
                                numLose++;
                                moneyLost = amountBet ;
                                cout<<"You lost $"<<amountBet <<endl;
                                system("pause");
                                break;
                        }
                           
               }
               
                if(!WorL){
                        
                        
                        system("cls");
                        cout<<"Your hand"<<endl;
                        player.displayHand() ;
                        cout<<"  ";
                        cout<<"The Dealers Hand."<<endl;
                        house.displayHand();
                        
                        
                        
                        if(player.getValue() > house.getValue()){   
                                                                            
                        //call win function from Outcome class.
                                  results.win(amountBet);
                                  WorL = true;
                                  numWins++;
                                  moneyWon = amountBet * 2;
                                  cout<<"You won $"<<amountBet * 2<<endl;
                        }
                        else if(player.getValue() < house.getValue()){
                             
                        //call lose function from  Outcome Class 
                             results.lose(amountBet);
                             WorL = true;
                             numLose++;
                             moneyLost = amountBet;
                             cout<<"You lost $"<<amountBet<<endl;
                            
                        }
                        else if(player.getValue() == house.getValue()){                  
                             cout<<"The game was a tie."<<endl;
                             cout<<"You didn't lose any money."<<endl;
                             tie++;  
                        }
                        system("pause");
                }
           }
            cout<<endl;          
            cout<<"Your new total of money is $"<<results.findMoney()<<endl<<endl;
            
             count++;
            
                 
            cout<<endl;                   
            cout<<"Would you like to play again.";
            cin>>Hresp;
            cout<<endl;
            money = results.findMoney(); 
            system("cls");
            
       }while(toupper(Hresp[0]) == 'Y');

	   cout<<"Would you like to save your game.";
            cin>>Hresp;
            
            if(toupper(Hresp[0]) == 'Y'){
                 outFile<<results.findMoney()<<endl;
                 outFile<<numWins<<endl;
                 outFile<<numLose<<endl;
                 outFile<<tie<<endl;
                 outFile<<moneyWon<<endl;
                 outFile<<moneyLost<<endl;
                 outFile<<count<<endl;//this is for how many games have been played.
				 
            }
    
	system("pause");
     
     return 0;
}
     
